package com.zls.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zls.mapper.EmployeeMapper;
import com.zls.pojo.Employee;
import com.zls.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper mapper;
    @Override
    public PageInfo<Employee> getEmpByPage(Integer pageindex, Integer pagesize) {
        PageHelper.startPage(pageindex,pagesize);
        List<Employee> emps= mapper.selectEmpAndDeptAll();
        PageInfo<Employee> pageInfo=new PageInfo<>(emps);
        return pageInfo;
    }
    @Override
    @Transactional
    public int addEmp(Employee employee) {
        int insert = mapper.insert(employee);
        if (insert > 0) {
            return insert;
        } else {
            return -1;
        }
    }

    @Override
    @Transactional
    public int editEmp(Employee employee) {
        int i = mapper.updateById(employee);
        if (i > 0) {
            return i;
        } else {
            return -1;
        }
    }

    @Override
    @Transactional
    public int dropEmpById(Integer id) {
        int i = mapper.deleteById(id);
        if (i > 0) {
            return i;
        } else {
            return -1;
        }
    }

    @Override
    public Employee getEmpById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<Employee> getAll() {
        return mapper.selectEmpAndDeptAll();
    }

}
